package cst.a917cqy.zyfypt_917cqy;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a912xjc.zyfypt_912xjc.R;



public class MainActivity extends AppCompatActivity {
    private LinearLayout  layout1,layout2,layout3,layout4;//用于界面切换
    private ImageButton ib1,ib2,ib3,ib4;//用户设置图片
    private ViewPager vp;
    FragmentPagerAdapter fragmentPagerAdapter;
    List<Fragment> listfragment;
private ResModel model;
    private cqyListener<List<VideoBean>> listener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      /*  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        model = new ResModel("gson");
        listener = new cqyListener<List<VideoBean>>() {
            @Override
            public void onResponse(List<VideoBean> l) {
                Toast.makeText(MainActivity.this, l.get(0).getName(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail(String msg) {
                System.out.print(msg);
            }
        };*/
        init();
        initFragment();
    }
        private void initFragment() {
            listfragment=new ArrayList<Fragment>();
            listfragment.add(new Fragment1());
            listfragment.add(new Fragment2());
            listfragment.add(new Fragment3());
            listfragment.add(new Fragment4());
            fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
                @Override
                public android.support.v4.app.Fragment getItem(int position) {
                    return listfragment.get(position);
                }

                @Override
                public int getCount() {
                    return listfragment.size();
                }
            };
            vp=(ViewPager)findViewById(R.id.viewPager);
            vp.setAdapter(fragmentPagerAdapter);
            vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    vp.setCurrentItem(position);
                    resetImage();
                    selectTab(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

        }
    View.OnClickListener onClicklistener =new View.OnClickListener(){
        @Override
        public void onClick(View view)
        {
           resetImage();
            switch (view.getId())
            {
                case R.id.layout1:
                    Toast.makeText(MainActivity.this,"切换底部栏Article",Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.layout2:
                    Toast.makeText(MainActivity.this,"切换底部栏课件",Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
                case R.id.layout3:
                    Toast.makeText(MainActivity.this,"切换底部栏视屏",Toast.LENGTH_SHORT).show();
                    selectTab(2);
                    break;
                case R.id.layout4:
                    Toast.makeText(MainActivity.this,"切换底部栏设置",Toast.LENGTH_SHORT).show();
                    selectTab(3);
                    break;
            }
        }
    };
        //model.getList("Video", listener);
    private  void init(){
        ib1=(ImageButton)findViewById(R.id.imageButton1);
        ib2=(ImageButton)findViewById(R.id.imageButton2);
        ib3=(ImageButton)findViewById(R.id.imageButton3);
        ib4=(ImageButton)findViewById(R.id.imageButton4);

        layout1=(LinearLayout)findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);

        layout1.setOnClickListener(onClicklistener);
        layout2.setOnClickListener(onClicklistener);
        layout3.setOnClickListener(onClicklistener);
        layout4.setOnClickListener(onClicklistener);
    }
    private void resetImage(){
        ib1.setImageResource(R.drawable.__picker_camera);
        ib2.setImageResource(R.drawable.__picker_ic_broken_image_black_48dp);
        ib3.setImageResource(R.drawable.address_normal);
        ib4.setImageResource(R.drawable.settings_normal);
    }
    private void selectTab(int i){
        switch (i){
            case 0:
                ib1.setImageResource(R.drawable.__picker_camera);
                break;
            case 1:
                ib2.setImageResource(R.drawable.__picker_ic_broken_image_black_48dp);
                break;
            case 2:
                ib3.setImageResource(R.drawable.address_pressed);
                break;
            case 3:
                ib4.setImageResource(R.drawable.settings_pressed);
                break;

        }
        vp.setCurrentItem(i);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
